package com.tiza.xiaoyoubao.ui.fragment;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tianze.library.view.SimpleDialog;
import com.tiza.xiaoyoubao.MainActivity;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.DianFuTbTaskOrderInfo;
import com.tiza.xiaoyoubao.entity.InsertTaskInfo;
import com.tiza.xiaoyoubao.entity.TbTaskOperateInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.BitmapUtils;
import com.tiza.xiaoyoubao.utils.DateUtils;
import com.tiza.xiaoyoubao.utils.ImageLoader;
import com.tiza.xiaoyoubao.utils.ImageUtils;
import com.tiza.xiaoyoubao.utils.OtherUtils;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import okhttp3.Call;
import okhttp3.Request;

import static android.app.Activity.RESULT_OK;

/**
 * Created by mapple on 17/4/14.
 */

public class DianFuTbOperationTaskFragment extends BaseFragment {

    DianFuTbTaskOrderInfo mDianFuTbTaskOrderInfo = Hawk.get(Constants.KEY_DIANFUTBTASKOEDER_INFO,
                                                            (DianFuTbTaskOrderInfo) null
    );
    InsertTaskInfo mInsertTaskInfo = Hawk.get(Constants.KEY_INSERTTASK_INFO, (InsertTaskInfo) null);
    private TbTaskOperateInfo taskOperateInfo;
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    @BindView(R.id.tv_shopName)
    TextView tv_shopName;
    @BindView(R.id.tv_contactShop)
    TextView tv_contactShop;
    @BindView(R.id.tv_shopDescribe)
    TextView tv_shopDescribe;
    @BindView(R.id.tv_showPrice)
    TextView tv_showPrice;
    @BindView(R.id.tv_cJPrice)
    TextView tv_cJPrice;
    @BindView(R.id.tv_additionalRequest)
    TextView tv_additionalRequest;

    @BindView(R.id.tv_referPrice)
    TextView tv_referPrice;

    @BindView(R.id.iv_image)
    ImageView iv_image;
    @BindView(R.id.iv_goalShop)
    ImageView iv_goalShop;
    @BindView(R.id.iv_goalShopEnd)
    ImageView iv_goalShopEnd;
    @BindView(R.id.iv_orderDetail)
    ImageView iv_orderDetail;
    @BindView(R.id.iv_openTaoBao)
    ImageView iv_openTaoBao;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.iv4)
    ImageView iv4;
    @BindView(R.id.iv5)
    ImageView iv5;
    @BindView(R.id.et_money)
    EditText et_money;
    @BindView(R.id.tv_comit)
    TextView tv_comit;

    /**
     * 选择图片的返回码
     */
    public final static int SELECT_IMAGE_RESULT_CODE = 200;
    String[] proj = {MediaStore.MediaColumns.DATA};
    private String ivState;
    private File imageGoalShop, imageGoalShopEnd, imageOrderDetail;
    private Bitmap bitmapGoalShop, bitmapGoalShopEnd, bitmapOrderDetail;
    /**
     * 当前选择的图片的路径
     */
    public String mImagePath;
    private final int REQUEST_IMAGE = 2;
    private int maxNum = 0;
    private ArrayList<String> mSelectPath;
    private List<Bitmap> bitmapList = new ArrayList<>();
    //是否选择的标记
    private boolean flag = true;
    private List<String> finalSelectPaths = new ArrayList<>();
    private int step = 0;
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Date date1;
    ArrayList<Date> dateList = new ArrayList<>();
    private String etMoney;
    Bundle args;
    private String way, copyContent, cid, orderid, addtime, buyerid, taskCode, reason, account, sellerID, endTime;
    private String taskState, orderFlag;
    private boolean isOverTime = false;
    Timer timer = new Timer();
    Date date;
    Date date2;
    long diff;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_operation_task_dainfu_tb;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        args = getArguments();
        way = args.getString("way");
        if (way.equals("1")) {
            taskState = "1";
            cid = String.valueOf(mInsertTaskInfo.getCid());
            orderid = mInsertTaskInfo.getOrderid();
            buyerid = mInsertTaskInfo.getBuyerid();
            account = mInsertTaskInfo.getAccount();
            sellerID = mInsertTaskInfo.getSellerid();
            addtime = DateUtils.getDateToStringSec(mInsertTaskInfo.getAddtime());
            endTime = DateUtils.getDateToStringSec(mInsertTaskInfo.getEndtime());
            timer.schedule(task, 1000, 1000);
        } else if (way.equals("2")) {
            taskState = "2";
            taskOperateInfo = (TbTaskOperateInfo) args.getSerializable("info");
            cid = String.valueOf(taskOperateInfo.getId());
            orderid = taskOperateInfo.getOrderid();
            taskCode = taskOperateInfo.getTaskCode();
            account = taskOperateInfo.getAccount();
            sellerID = taskOperateInfo.getSellerId();
            addtime = DateUtils.getDateToStringSec(taskOperateInfo.getAddTime());
            endTime = DateUtils.getDateToStringSec(taskOperateInfo.getEndTime());
            timer.schedule(task, 1000, 1000);
        }
    }


    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        date = df.parse(df.format(new Date()));
                        date2 = df.parse(endTime);

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    diff = (date2.getTime() - date.getTime()) / 1000;
                    int mm = (int) diff;
                    tv_comit.setText("提交任务:" + "(任务剩余时间 " + mm + "S)");
                    if (diff <= 0) {
                        timer.cancel();
                        isOverTime = true;
                    }
                }
            });
        }
    };

    @Override
    protected void initData() {
        try {
            date1 = df.parse(addtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initView() {
        if (way.equals("1")) {
            tv_shopName.setText(mDianFuTbTaskOrderInfo.getShopName()
                    .replaceFirst(mDianFuTbTaskOrderInfo.getShopName().substring(0, 1),
                            "*"
                    ));
            ImageLoader.getInstance().displayImage(
                    ApiHttpClient.URL2 + mDianFuTbTaskOrderInfo.getImages(), iv_image);
            if (!TextUtils.isEmpty(mDianFuTbTaskOrderInfo.getStepImage1()) && mDianFuTbTaskOrderInfo
                    .getStepImage1() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + mDianFuTbTaskOrderInfo.getStepImage1(), iv1);
            }
            if (!TextUtils.isEmpty(mDianFuTbTaskOrderInfo.getStepImage2()) && mDianFuTbTaskOrderInfo
                    .getStepImage2() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + mDianFuTbTaskOrderInfo.getStepImage1(), iv2);
            }
            if (!TextUtils.isEmpty(mDianFuTbTaskOrderInfo.getStepImage3()) && mDianFuTbTaskOrderInfo
                    .getStepImage3() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + mDianFuTbTaskOrderInfo.getStepImage1(), iv3);
            }
            if (!TextUtils.isEmpty(mDianFuTbTaskOrderInfo.getStepImage4()) && mDianFuTbTaskOrderInfo
                    .getStepImage4() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + mDianFuTbTaskOrderInfo.getStepImage1(), iv4);
            }
            if (!TextUtils.isEmpty(mDianFuTbTaskOrderInfo.getStepImage5()) && mDianFuTbTaskOrderInfo
                    .getStepImage5() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + mDianFuTbTaskOrderInfo.getStepImage1(), iv5);
            }
            if (mDianFuTbTaskOrderInfo.getTitle().length() <= 15) {
                tv_shopDescribe.setText(mDianFuTbTaskOrderInfo.getTitle());
            } else {
                tv_shopDescribe.setText("***" + mDianFuTbTaskOrderInfo.getTitle()
                                                                      .substring(15,
                                                                                 mDianFuTbTaskOrderInfo
                                                                                         .getTitle()
                                                                                         .length()
                                                                      ));
            }
            tv_cJPrice.setText(mDianFuTbTaskOrderInfo.getPrice());
            tv_showPrice.setText(mDianFuTbTaskOrderInfo.getMobilePrice());
            tv_additionalRequest.setText(mDianFuTbTaskOrderInfo.getRemarks() + "");
            tv_referPrice.setText(
                    "应垫付金额参考:" + mDianFuTbTaskOrderInfo.getMobilePrice() + "元(请按照实际垫付金额填写)");
            if (TextUtils.isEmpty(mDianFuTbTaskOrderInfo.getTcode()) && TextUtils.isEmpty(
                    mDianFuTbTaskOrderInfo.getTlink())) {
                iv_openTaoBao.setVisibility(View.GONE);
            } else {
                iv_openTaoBao.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mDianFuTbTaskOrderInfo.getTcode())) {
                    copyContent = mDianFuTbTaskOrderInfo.getTcode();
                } else {
                    copyContent = mDianFuTbTaskOrderInfo.getTlink();
                }
            }

        } else if (way.equals("2")) {
            tv_shopName.setText(taskOperateInfo.getShopName()
                    .replaceFirst(taskOperateInfo.getShopName().substring(0, 1),
                            "*"
                    ));
            ImageLoader.getInstance().displayImage(
                    ApiHttpClient.URL2 + taskOperateInfo.getImages(), iv_image);
            if (!TextUtils.isEmpty(taskOperateInfo.getStepImage1()) && taskOperateInfo
                    .getStepImage1() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + taskOperateInfo.getStepImage1(), iv1);
            }
            if (!TextUtils.isEmpty(taskOperateInfo.getStepImage2()) && taskOperateInfo
                    .getStepImage2() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + taskOperateInfo.getStepImage1(), iv2);
            }
            if (!TextUtils.isEmpty(taskOperateInfo.getStepImage3()) && taskOperateInfo
                    .getStepImage3() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + taskOperateInfo.getStepImage1(), iv3);
            }
            if (!TextUtils.isEmpty(taskOperateInfo.getStepImage4()) && taskOperateInfo
                    .getStepImage4() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + taskOperateInfo.getStepImage1(), iv4);
            }
            if (!TextUtils.isEmpty(taskOperateInfo.getStepImage5()) && taskOperateInfo
                    .getStepImage5() != null) {
                ImageLoader.getInstance().displayImage(
                        ApiHttpClient.URL2 + taskOperateInfo.getStepImage1(), iv5);
            }
            if (taskOperateInfo.getTitle().length() <= 15) {
                tv_shopDescribe.setText(taskOperateInfo.getTitle());
            } else {
                tv_shopDescribe.setText("***" + taskOperateInfo.getTitle()
                        .substring(15,
                                mDianFuTbTaskOrderInfo
                                        .getTitle()
                                        .length()
                        ));
            }
            tv_cJPrice.setText(taskOperateInfo.getPrice());
            tv_showPrice.setText(taskOperateInfo.getMobilePrice());
            tv_additionalRequest.setText(taskOperateInfo.getRemarks() + "");
            tv_referPrice.setText(
                    "应垫付金额参考:" + taskOperateInfo.getMobilePrice() + "元(请按照实际垫付金额填写)");
            if (TextUtils.isEmpty(taskOperateInfo.getTcode()) && TextUtils.isEmpty(
                    taskOperateInfo.getTlink())) {
                iv_openTaoBao.setVisibility(View.GONE);
            } else {
                iv_openTaoBao.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(taskOperateInfo.getTcode())) {
                    copyContent = taskOperateInfo.getTcode();
                } else {
                    copyContent = taskOperateInfo.getTlink();
                }
            }
        }
        intWatch();
    }

    @OnClick({
            R.id.view_comit,
            R.id.iv_openTaoBao,
            R.id.iv_goalShop,
            R.id.iv_goalShopEnd,
            R.id.iv_orderDetail,
            R.id.view_save
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_comit:
                if (isOverTime) {
                    toast("无法提交，任务已超时");
                } else {
                    String message = checkMessage();
                    if (!TextUtils.isEmpty(message)) {
                        toast(message);
                        return;
                    }
                    insertDianFuTBTask();
                }
                break;
            case R.id.iv_openTaoBao:
                OtherUtils.copy(copyContent, mActivity);
                try {
                    PackageManager packageManager = mActivity.getPackageManager();
                    Intent intent = new Intent();
                    intent = packageManager.getLaunchIntentForPackage("com.taobao.taobao");
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    toast("您未安装手机淘宝客户端,请先安装");
                }
                break;
            case R.id.iv_goalShop:
                upLoadImage("goalShop");
                break;
            case R.id.iv_goalShopEnd:
                upLoadImage("goalShopEnd");
                break;
            case R.id.iv_orderDetail:
                upLoadImage("orderDetail");
                break;
            case R.id.view_save:
                maxNum = 3;
                selectGallery();
                break;
            default:
                break;
        }
    }

    public void upLoadImage(final String imageState) {
        ivState = imageState;
        pickPhoto();
    }

    /***
     * 从相册中取图片
     */
    public void pickPhoto() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        getActivity().startActivityForResult(intent, SELECT_IMAGE_RESULT_CODE);
    }

    private void selectGallery() {
        if (bitmapList != null || bitmapList.size() > 0) {
            bitmapList.clear();
        }
        if (finalSelectPaths != null || finalSelectPaths.size() > 0) {
            finalSelectPaths.clear();
        }
        if (dateList != null || dateList.size() > 0) {
            dateList.clear();
        }
        Intent intent = new Intent(mActivity, MultiImageSelectorActivity.class);
        // 是否显示拍摄图片,不显示
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, false);
        // 最大可选择图片数量
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);
        // 选择模式,选取多张
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE,
                        MultiImageSelectorActivity.MODE_MULTI
        );
        //        // 默认选择
        //        if (mSelectPath != null && mSelectPath.size() > 0) {
        //            intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
        //        }
        getActivity().startActivityForResult(intent, REQUEST_IMAGE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_IMAGE_RESULT_CODE && resultCode == RESULT_OK) {
            String imagePath = "";
            Uri uri = null;
            if (data != null && data.getData() != null) {// 有数据返回直接使用返回的图片地址
                uri = data.getData();
                Cursor cursor = getActivity().getContentResolver().query(uri, proj, null, null,
                                                                         null
                );
                if (cursor == null) {
                    uri = ImageUtils.getUri(getActivity(), data);
                }
                imagePath = ImageUtils.getFilePathByFileUri(getActivity(), uri);
            } else {// 无数据使用指定的图片路径
                imagePath = mImagePath;
                int i = ImageUtils.parseImageDegree(imagePath);
                System.out.println("旋转的角度:" + i);

            }
            File file = new File(imagePath);
            Date date = new Date(file.lastModified());
            if ((date.getTime() - date1.getTime()) / 1000 < 0) {
                toast("请上传任务开始后的图片");
                return;
            }
            //获取图片缩略图，避免OOM
            Bitmap bitmap = ImageUtils.getImageThumbnail(imagePath, ImageUtils.getWidth(
                    getActivity().getApplicationContext()) / 3 - 5, ImageUtils.getWidth(
                    getActivity().getApplicationContext()) / 3 - 5);
            if (ivState.equals("goalShop")) {
                imageGoalShop = new File(imagePath);
                bitmapGoalShop = ImageUtils.rotateBitmap(bitmap,
                                                         ImageUtils.parseImageDegree(imagePath)
                );
                iv_goalShop.setImageBitmap(bitmapGoalShop);
            } else if (ivState.equals("goalShopEnd")) {
                imageGoalShopEnd = new File(imagePath);
                bitmapGoalShopEnd = ImageUtils.rotateBitmap(bitmap,
                                                            ImageUtils.parseImageDegree(imagePath)
                );
                iv_goalShopEnd.setImageBitmap(bitmapGoalShopEnd);

            } else if (ivState.equals("orderDetail")) {
                imageOrderDetail = new File(imagePath);
                bitmapOrderDetail = ImageUtils.rotateBitmap(bitmap,
                                                            ImageUtils.parseImageDegree(imagePath)
                );
                iv_orderDetail.setImageBitmap(bitmapOrderDetail);
            }
        } else if (requestCode == REQUEST_IMAGE && resultCode == RESULT_OK) {
            mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            for (String p : mSelectPath) {
                //加入最后选择的集合
                finalSelectPaths.add(p);
                Log.e("路径", p);
                //加载缩略图
                Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromSd(p, 200, 240);
                bitmapList.add(bitmap);
            }
            for (int i = 0; i < finalSelectPaths.size(); i++) {
                File file = new File(finalSelectPaths.get(i));
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                Date date = new Date(file.lastModified());
                dateList.add(date);
                Log.i("截图时间", "截图文件：" + finalSelectPaths.get(i));
                Log.i("截图时间", "截图时间：" + df.format(date));
            }
            Log.i("路径长度", "存储时间长度：" + dateList.size() + "");
            Log.i("路径长度", "路径长度：" + finalSelectPaths.size() + "");
            Log.i("路径长度", "bitmapList长度：" + bitmapList.size() + "");

            if ((dateList.get(0).getTime() - date1.getTime()) / 1000 > 0 && (dateList.get(1)
                                                                                     .getTime() - date1
                    .getTime()) / 1000 > 0 && (dateList.get(2)
                                                       .getTime() - date1.getTime()) / 1000 > 0) {
                imageGoalShop = new File(finalSelectPaths.get(0));
                imageGoalShopEnd = new File(finalSelectPaths.get(1));
                imageOrderDetail = new File(finalSelectPaths.get(2));
                iv_goalShop.setImageBitmap(bitmapList.get(0));
                iv_goalShopEnd.setImageBitmap(bitmapList.get(1));
                iv_orderDetail.setImageBitmap(bitmapList.get(2));
                bitmapGoalShop = bitmapList.get(0);
                bitmapGoalShopEnd = bitmapList.get(1);
                bitmapOrderDetail = bitmapList.get(2);
            } else {
                toast("请上传任务开始后的图片");
                return;
            }
        }
    }

    private String checkMessage() {
        if (bitmapGoalShop == null || bitmapOrderDetail == null) return "请选择要上传的照片";
        etMoney = et_money.getText().toString().trim();
        if (TextUtils.isEmpty(etMoney)) return "请输入实付金额";
        return "";
    }

    private void upDateTask() {
        ApiHttpClient.setUpdateTask(this, orderid, orderFlag,
                                    new JsonCallback<String>(String.class, true) {

                                        @Override
                                        public void onBefore(Request request) {
                                            super.onBefore(request);
                                        }

                                        @Override
                                        public void onError(Call call, Exception e) {
                                            toast("网络连接失败,请稍后再试");
                                        }

                                        @Override
                                        public void onResponse(ResponseInfo<String> response) {
                                            int code = response.getCode();
                                            String message = response.getMessage();
                                            if (code == 1) {
                                                showWarnDialog(message);
                                                return;
                                            }
                                            Map<String, Object> _map = response.getFirstEntity();
                                            Bundle args = new Bundle();
                                            if (taskState.equals("1")) {
                                                args.putSerializable("info",
                                                                     mDianFuTbTaskOrderInfo
                                                );
                                                args.putString("state", "orderTask");
                                                args.putString("time", addtime);
                                                args.putString("orderid", orderid);
                                                args.putString("buyerid", buyerid);
                                                UIHelper.showDianFuTbTask2(mActivity, args);
                                            } else if (taskState.equals("2")) {
                                                args.putSerializable("info", taskOperateInfo);
                                                args.putString("state", "operate");
                                                args.putString("time", addtime);
                                                UIHelper.showDianFuTbTask2(mActivity, args);
                                            } else if (taskState.equals("3")) {
                                                args.putString("way", "2");
                                                UIHelper.showTbUnComplate(mActivity, args);
                                            }
                                            finish();
                                        }

                                        @Override
                                        public void onAfter() {
                                            super.onAfter();
                                        }
                                    }
        );
    }

    @Override
    public boolean onBackPressed() {
        showCustomConfirmDialog("返回后台后将不保存所做操作，下次需要重新上传截图哦，确定要返回吗", "确定", "取消",
                                new SimpleDialog.OnSimpleClickListener() {
                                    @Override
                                    public void onClick(SimpleDialog simpleDialog) {
                                        if (isOverTime) {
                                            UIHelper.show(getActivity(), MainActivity.class, true);
                                            finish();
                                        } else {
                                            orderFlag = "1";
                                            upDateTask();
                                        }
                                        simpleDialog.dismiss();
                                    }
                                }, new SimpleDialog.OnSimpleClickListener() {
                    @Override
                    public void onClick(SimpleDialog simpleDialog) {
                        simpleDialog.dismiss();
                    }
                }
        );
        return true;
    }

    public void intWatch() {
        et_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                et_money.setTextColor(getResources().getColor(R.color.textColorThird));
            }
        });

    }

    private void insertDianFuTBTask() {
        ApiHttpClient.insertDianFuTBTask(this, cid, orderid,
                                         userInfo.getWebzzCmsBuyerInfo().getUserid(), etMoney,
                                         imageGoalShop, imageGoalShopEnd, imageOrderDetail, addtime,
                                         new JsonCallback<String>(String.class, true) {

                                             @Override
                                             public void onBefore(Request request) {
                                                 super.onBefore(request);
                                                 showWaitingDialog();
                                             }

                                             @Override
                                             public void onError(Call call, Exception e) {
                                                 toast("网络异常，请检查后再试");
                                             }

                                             @Override
                                             public void onResponse(ResponseInfo<String> response) {
                                                 int code = response.getCode();
                                                 if (code == 1) {
                                                     showWarnDialog(response.getMessage());
                                                     return;
                                                 }
                                                 toast("任务提交成功");
                                                 orderFlag = "3";
                                                 taskState = "3";
                                                 upDateTask();
                                             }

                                             @Override
                                             public void onAfter() {
                                                 super.onAfter();
                                                 hideWaitingDialog();
                                             }
                                         }
        );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
